/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gestionedt.enseignants;

import gestionedt.controllers.EnseignantJpaController;
import gestionedt.models.Creneau;
import gestionedt.models.Enseignant;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

/**
 *
 */
public class EnseignantsAction extends DispatchAction {
    
    /**
     * Liste tous les enseignants.
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws java.lang.Exception
     */
    public ActionForward lister(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        EnseignantsForm eForm = (EnseignantsForm) form;
        EnseignantJpaController ct = new EnseignantJpaController();
        eForm.setEnseignants(ct.getList());

        return mapping.findForward("liste");
    }

    /**
     * Affiche le formulaire d'ajout d'un nv enseignant.
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws java.lang.Exception
     */
    public ActionForward afficherNv(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        return mapping.findForward("formEnseignant");
    }

    /**
     * Affiche le formulaire de modification d'un enseignant existant.
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws java.lang.Exception
     */
    public ActionForward afficher(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        EnseignantsForm eForm = (EnseignantsForm) form;
        int idEnseignant = eForm.getIdEnseignant();
        EnseignantJpaController ctEns = new EnseignantJpaController();
        Enseignant ens = ctEns.findEnseignantAvecRel(idEnseignant);
        eForm.setEnseignant(ens);

        String[] numsIndispo = new String[ens.getIndisponibilites().size()];
        int i = 0;
        for(Creneau c : ens.getIndisponibilites())
            numsIndispo[i++] = c.getNumero().toString();
        eForm.setNumsIndispo(numsIndispo);

        return mapping.findForward("formEnseignant");
    }

    /**
     * Supprime un enseignant.
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws java.lang.Exception
     */
    public ActionForward supprimer(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        EnseignantsForm eForm = (EnseignantsForm) form;
        int idEnseignant = eForm.getIdEnseignant();
        EnseignantJpaController ctEns = new EnseignantJpaController();
        ctEns.destroy(idEnseignant);

        return mapping.findForward("lister");
    }
    
}